package SmartHome.View;

import SmartHome.Main;
import SmartHome.Session;
import SmartHome.util.DialogUtils;
import SmartHome.util.JdbcUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class VendorController {
    public TextField usernameField;
    public PasswordField passwordField;
    public CheckBox isSystemAdmin;
    public BorderPane DevicePane;
    public BorderPane TypePane;
    public Button typeAdd;
    public Button typeDelete;
    public Button typeUpdate;
    public Button typeSearch;
    public BorderPane searchPane;
    public Button divAdd;
    public Button divDelete;
    public Button divUpdate;
    public Button divSearch;
    public Main MainApp;
    public TextField typeIDField;
    public TextField typeNameField;
    public TextField dNameField;
    public TextField dIDField;
    public TextField dTypeField;
    public TextField dDesField;
    public TextField dVendorField;
    public Label userNameLabel;
    public Label deviceManage;
    public Label typeManage;
    public Label dataManage;
    public Label exit;
    public HBox filterBox;
    public ComboBox seMagazineClassBox;
    public Button filterButton;
    public HBox searchBox;
    public Label searchLabel;
    public ComboBox seCopiesNumBox;
    public Button searchButton;
    public TableView seTableView;
    public HBox filterBoxc;
    public HBox searchBox3;
    public Label searchLabel3;
    public HBox filterBoxb;
    public BorderPane searchPaneb;
    public HBox searchBox2;
    public Label searchLabel2;
    public BorderPane searchPanec;
    public BorderPane searchPanea;
    public HBox filterBoxa;
    public HBox searchBox1;
    public Label searchLabel1;
    public TextField dVenderField;
    public TextField dTypeIdField;
    public TextField dFamIdField;

    @FXML
    private TableColumn<?, ?> seOrderIdColumn;

    @FXML
    private TableColumn<?, ?> seMagaNameColumn;

    @FXML
    private TableColumn<?, ?> seUserNameColumn;

    @FXML
    private TableColumn<?, ?> seCopiesNumColumn;

    @FXML
    private TableColumn<?, ?> seMonthColumn;

    @FXML
    private TableColumn<?, ?> seTotalPriceColumn;


    public void tAddAction(ActionEvent actionEvent) {
    }

    public void tDeleteAction(ActionEvent actionEvent) {
    }

    public void tUpdateAction(ActionEvent actionEvent) {
    }

    public void tSearchAction(ActionEvent actionEvent) {
    }

    public void setMainApp(Main MainApp) {
        this.MainApp = MainApp;
    }

    public void handleToData(javafx.scene.input.MouseEvent mouseEvent) {
        DevicePane.setVisible(false);
        TypePane.setVisible(false);
        //initTypeTable();
        searchPane.setVisible(true);
    }

    public void handleToDevice(javafx.scene.input.MouseEvent mouseEvent) {
        TypePane.setVisible(false);
        searchPane.setVisible(false);
        //initDeviceTable();
        DevicePane.setVisible(true);
    }

    public void handleToType(javafx.scene.input.MouseEvent mouseEvent) {
        DevicePane.setVisible(false);
        searchPane.setVisible(false);
        //initTypeTable();
        TypePane.setVisible(true);
        System.out.println(Session.getId());
    }

    public void handleLogin() throws Exception {
        Connection conn = null;
        String sql = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        if (isSystemAdmin.isSelected()) {
            sql = "select * from `Administrators` where AdminId = ? and AdminPassword = ? and AdminType =1";
            Session.setId(Integer.parseInt(usernameField.getText()));
            System.out.println(Session.getId());
        } else {
            sql = "select * from `Administrators` where AdminId = ? and AdminPassword = ? and AdminType =0";
            Session.setId(Integer.parseInt(usernameField.getText()));
            System.out.println(Session.getId());
        }
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.parseInt(usernameField.getText()));
            ps.setInt(2, Integer.parseInt(passwordField.getText()));
            rs = ps.executeQuery();
            if (rs.next()) {
                Stage stage = new Stage();
                if (isSystemAdmin.isSelected()) {
                    Parent root = FXMLLoader.load(getClass().getResource("Admin.fxml"));
                    Scene scene = new Scene(root);
                    stage.setScene(scene);
                    stage.show();
                } else {
                    Parent root = FXMLLoader.load(getClass().getResource("vendor.fxml"));
                    Scene scene = new Scene(root);
                    stage.setScene(scene);
                    stage.show();
                }
            } else {
                throw new NullPointerException("Wrong Information! Try Again!");
            }
        } catch (Exception e) {
            DialogUtils.tips(MainApp.getPrimaryStage(), "Your id and password are not match" +
                    "or your imformation is not implement. ");
            usernameField.setText("");
            passwordField.setText("");
            usernameField.requestFocus();
        } finally {
            JdbcUtil.close(conn, ps, rs);
        }

    }
    public void exitAction(javafx.scene.input.MouseEvent mouseEvent) {
    }

    public void dAddAction(ActionEvent actionEvent) {
    }

    public void dDeleteAction(ActionEvent actionEvent) {
    }

    public void dUpdateAction(ActionEvent actionEvent) {
    }

    public void dSearchAction(ActionEvent actionEvent) {
    }
}
